package com.leeh.jsontest;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import java.net.URL;
import java.util.Map;

/**
 * Created by Liheng on 2017-04-16.
 */

public class DataUtils {

    /**
     * 获取验证码
     *
     * @param handler
     * @param urlCookie
     */
    public static void getVerCode(final Handler handler, final String urlCookie) {
        new Thread() {
            @Override
            public void run() {
                try {
                    Network network = new Network(new URL(urlCookie), Network.METHOD_GET);
                    Message msg = new Message();
                    if (network.connectNetwork()) {
                        byte[] imageByte = network.getBytes();
                        Bundle bundle = new Bundle();
                        bundle.putByteArray("verCode", imageByte);
                        msg.what = Constents.GET_VERCODER;
                        msg.setData(bundle);
                    } else {
                        msg.what = Constents.GET_DISABLE_NETWORK;
                    }
                    handler.sendMessage(msg);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    /**
     * 从网上获取课程信息
     *
     * @param handler
     * @param urlCourse 网络初始化参数
     * @param dataParams    查询数据的参数
     */
    public static void searchFromNetworkCourse(final Handler handler, final String urlCourse, final Map<String, String> dataParams) {
        new Thread() {
            @Override
            public void run() {
                try {
                    Network network = new Network(new URL(urlCourse), Network.METHOD_POST);
                    Message msg = new Message();
                    String html = network.getData(dataParams, Network.EDIT_CODE_UTF_8);
                    msg.what = Constents.GET_COURSE;
                    msg.obj = html;
                    handler.sendMessage(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    /**
     * 获取网页cookie
     *
     * @param handler
     * @param urlAddress
     */
    public static void getCookie(final Handler handler, final String urlAddress) {
        new Thread() {
            @Override
            public void run() {
                try {
                    Network network = new Network(new URL(urlAddress), Network.METHOD_GET);
                    Message msg = new Message();
                    if (network.connectNetwork()) {
                        String cookie = network.getCookie();
                        msg.what = Constents.GET_COOKIE;
                        msg.obj = cookie;
                    } else {
                        msg.what = Constents.GET_DISABLE_NETWORK;
                    }
                    handler.sendMessage(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    /**
     * 获取页面下拉列表内容
     *
     * @param handler
     * @param urlAddress
     */
    public static void getOptionInfoFromNetWork(final Handler handler, final String urlAddress, final int type) {
        new Thread() {
            @Override
            public void run() {
                Network network;
                try {
                    network = new Network(new URL(urlAddress), Network.METHOD_GET);
                    Message msg = new Message();
                    if (network.connectNetwork()) {
                        String html = network.getData(Network.EDIT_CODE_UTF_8);
                        Bundle bundle = new Bundle();
                        bundle.putInt("type", type);
                        msg.setData(bundle);
                        msg.what = Constents.GET_SPINNER_CONTENT;
                        msg.obj = html;
                        msg.arg1 = type;
                    } else {
                        msg.what = Constents.GET_DISABLE_NETWORK;
                    }
                    handler.sendMessage(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }


}
